const Router=require("koa-router")
const router=new Router()
const axios=require("axios")
router.get("/oauth/giteeRedirectUri",async ctx=>{
      //用户点击可以拿到授权吗
  let code=ctx.query.code
  console.log(code)
  //https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
  let client_id='858434d2e018d39cf53fd56d72671cebfc57e5fd456854f8d9988162b92b7cb6'
  let redirect_uri='http://localhost:3000/oauth/giteeRedirectUri'
  let client_secret='d8c54acd4822aefd3d86caabb8cdb321c913d577e61d3f50ac5b5da2870283b8'
  if(code){
    let option={
        method: 'post',
        url: 'https://gitee.com/oauth/token',
        data: {
            grant_type:'authorization_code',
            code,
            client_id,
            redirect_uri,
            client_secret
        },
    }
    let {data}=await axios(option)
    // console.log(data)
    const data1=await axios({
        method: 'get',
        url: 'https://gitee.com/api/v5/user',
        params: {
            access_token:data.access_token
        }
    })
   
    const {name,avatar_url}=data1.data
    ctx.body={data:name,img:avatar_url}
  }
})

module.exports=router